package com.itcast.stargraph.service;

import com.itcast.stargraph.pojo.ComfyuiModel;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * 工作流JSON渲染Service接口类
 *
 * @Author katana
 * @Date 2025/6/11 11:09
 */
@Component
public class FreemarkerService {
    @Autowired
    private Configuration  configuration;
    
    public String renderWorkFlowJSON(ComfyuiModel model) {
        try {
            //1 获取模版对象
            Template template = configuration.getTemplate("workflow.ftl");
            //2 准备数据
            Map<String, Object> modleMap = new HashMap<>();
            modleMap.put("config", model);
            //3 创建输出流
            StringWriter outputWriter = new StringWriter();
            //4 合并数据
            template.process(modleMap, outputWriter);
            
            //5 得到渲染结果
            return outputWriter.toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
